<template>
  <!-- class="app-container" -->
  <div>
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="优先级" prop="priorityLevel">
        <el-input
          v-model.trim="queryParams.priorityLevel"
          placeholder="请输入优先级"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="引用类型" prop="refType">
        <el-input
          v-model.trim="queryParams.refType"
          placeholder="请输入引用类型"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="引用id" prop="userRefId">
        <el-input
          v-model.trim="queryParams.userRefId"
          placeholder="请输入引用id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
      <br />
      <el-form-item label="数据表名" prop="tableName">
        <!-- <el-input
          v-model.trim="queryParams.tableName"
          placeholder="请输入数据表名"
          clearable
          @keyup.enter.native="handleQuery"
        /> -->
        <el-select
          v-model="queryParams.tableName"
          clearable
          filterable
          placeholder="请输入数据表名"
          @change="selectlistByTableName"
        >
          <el-option
            v-for="item in TableNameList"
            :key="item"
            :label="item"
            :value="item"
          >
          </el-option>
        </el-select>
      </el-form-item>

      <!-- <el-form-item label="逻辑删除" prop="isDelete">
        <el-input
          v-model.trim="queryParams.isDelete"
          placeholder="请输入逻辑删除"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="selectlistByTableName"
          >搜索</el-button
        >
        <!-- <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        > -->
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['conf:BiRuleTagGroup:add']"
          >新增</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['conf:BiRuleTagGroup:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['conf:BiRuleTagGroup:remove']"
          >删除</el-button
        >
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['conf:BiRuleTagGroup:export']"
          >导出</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          size="mini"
          @click="handleupdateByStrategyCode"
          v-if="isShowSave"
          >保存</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      :data="BiRuleTagGroupList"
      @selection-change="handleSelectionChange"
      @cell-click="tableEdit"
      :height="300"
    >
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="是否选择" align="center" prop="isSelected">
        <template slot-scope="scope">
          <el-checkbox
            true-label="0"
            false-label="1"
            v-model="scope.row.isSelected"
          ></el-checkbox
        ></template>
      </el-table-column>
      <el-table-column label="标签组id" align="center" prop="id" />
      <el-table-column label="优先级" align="center" prop="priorityLevel" />
      <!-- <el-table-column label="引用类型" align="center" prop="refType" />
      <el-table-column label="引用id" align="center" prop="userRefId" /> -->
      <el-table-column
        width="220"
        label="标签组描述"
        align="center"
        prop="remark"
      />
      <el-table-column label="是否激活" align="center">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.isActive"
            active-value="0"
            inactive-value="1"
            @change="handleStatusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <!-- <el-table-column label="逻辑删除" align="center" prop="isDelete" /> -->

      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['conf:BiRuleTagGroup:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['conf:BiRuleTagGroup:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改BI打标标签组对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="优先级" prop="priorityLevel">
          <el-input
            v-model.trim="form.priorityLevel"
            placeholder="请输入优先级"
          />
        </el-form-item>
        <el-form-item label="引用类型" prop="refType">
          <el-input
            v-model.trim="form.refType"
            placeholder="请输入引用类型"
            clearable
          />
        </el-form-item>
        <el-form-item label="引用id" prop="userRefId">
          <el-input v-model.trim="form.userRefId" placeholder="请输入引用id" />
        </el-form-item>
        <!-- <el-form-item label="是否激活" prop="isActive">
          <el-input v-model.trim="form.isActive" placeholder="请输入是否激活" />
        </el-form-item> -->
        <!-- <el-form-item label="逻辑删除" prop="isDelete">
          <el-input v-model.trim="form.isDelete" placeholder="请输入逻辑删除" />
        </el-form-item> -->
        <el-form-item label="备注" prop="remark">
          <el-input v-model.trim="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listBiRuleTagGroup,
  getBiRuleTagGroup,
  delBiRuleTagGroup,
  addBiRuleTagGroup,
  updateBiRuleTagGroup,
  listByTableName,
  updateTagGroupRefRelation,
} from "@/api/conf/BiRuleTagGroup";
import {
  listBiruletag,
  getBiruletag,
  delBiruletag,
  addBiruletag,
  updateBiruletag,
  activeRule,
  selectTableNameList,
} from "@/api/conf/biruletag";
export default {
  name: "BiRuleTagGroup",
  props: {
    row: {
      type: Object,
      default: () => {},
    },
  },
  data() {
    return {
      isShowSave: true,
      TableNameList: [],
      inputValue: "",
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // BI打标标签组表格数据
      BiRuleTagGroupList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        priorityLevel: null,
        refType: null,
        userRefId: null,
        isActive: null,
        isDelete: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    selectTableNameList().then((response) => {
      this.TableNameList = response;
    });
  },
  watch: {
    row: {
      handler: function (newnew, oldold) {
        console.log("newnew", newnew);
        if (JSON.stringify(this.row) != "{}") {
          this.queryParams.tableName = this.row.sqlTableName;
          this.queryParams.birelationId = this.row.id;

          this.selectlistByTableName();
          // this.gettableNameList();
        } else {
          this.getList();
        }
      },
      deep: true,
      immediate: true,
    },
  },
  methods: {
    selectlistByTableName() {
      listByTableName(this.queryParams).then((response) => {
        this.BiRuleTagGroupList = response.rows;
        this.total = response.total;
        this.$emit("handletagGroupId", response.rows[0]);
      });
    },
    handleupdateByStrategyCode() {
      let purchaseOrderNumbers = [];
      this.BiRuleTagGroupList.forEach((item) => {
        if (item.isSelected == "0") {
          purchaseOrderNumbers.push(item.id);
        }
      });
      let obj = {
        biRelationshipTableId: this.row.id,
        biTagGroupIdList: purchaseOrderNumbers,
      };
      updateTagGroupRefRelation(obj).then((response) => {
        if (response.code == 200) {
          this.$modal.msgSuccess("保存成功");
        } else {
          this.$modal.msgError("保存失败");
        }
      });
    },
    tableEdit(row) {
      this.$emit("handletagGroupId", row);
    },
    // 用户状态修改
    handleStatusChange(row) {
      let text = row.isActive === "0" ? "启用" : "停用";
      this.$modal
        .confirm('确认要"' + text + '"规则吗？')
        .then(function () {
          return updateBiRuleTagGroup(row);
        })
        .then(() => {
          this.$modal.msgSuccess(text + "成功");
        })
        .catch(function () {
          row.isActive = row.isActive === "0" ? "1" : "0";
        });
    },
    /** 查询BI打标标签组列表 */
    getList() {
      listBiRuleTagGroup(this.queryParams).then((response) => {
        this.BiRuleTagGroupList = response.rows;
        this.total = response.total;
        this.$emit("handletagGroupId", response.rows[0]);
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        priorityLevel: null,
        refType: null,
        userRefId: null,
        isActive: "0",
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        isDelete: null,
        remark: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.queryParams.pageSize = 10;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams = {};
      this.isShowSave = false;
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加BI打标标签组";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getBiRuleTagGroup(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改BI打标标签组";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateBiRuleTagGroup(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBiRuleTagGroup(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除BI打标标签组编号为"' + ids + '"的数据项？')
        .then(function () {
          return delBiRuleTagGroup(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "conf/BiRuleTagGroup/export",
        {
          ...this.queryParams,
        },
        `BiRuleTagGroup_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
